package com.dxy.app.widget.universal_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.dxy.app.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 描 叙: 自适应内容的view
 * 作 者: duxingyu
 * 时 间: 2017/8/10 10:02
 **/

public class ContentWrapView extends ViewGroup {

  private List<String> list;
  public ContentWrapView(Context context) {
    super(context);
    initParam(null);
  }

  public ContentWrapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.ContentWrapView);
    initParam(array);
  }

  public ContentWrapView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.ContentWrapView);
    initParam(array);
  }

  private Drawable itemDrawable;
  private int marTop,marLeft;
  private void initParam(TypedArray array){
    int itemResource;
    if(array!=null){
      itemResource =array.getResourceId(R.styleable.ContentWrapView_wrap_item_background,R.drawable.selector_item_circle_bg);
      marLeft =array.getDimensionPixelOffset(R.styleable.ContentWrapView_wrap_item_margin_horizontal,20);
      marTop =array.getDimensionPixelOffset(R.styleable.ContentWrapView_wrap_item_margin_vertical,20);
    }else{
      float density =getResources().getDisplayMetrics().density;
      itemResource = R.drawable.selector_item_circle_bg;
      marTop = (int) (10*density);
      marLeft = (int) (10*density);
    }
    itemDrawable = getResources().getDrawable(itemResource);
    init();
  }
  private void init(){
    list =new ArrayList<>();
    list.add("哈算法");
    list.add("哈佛四儿浸泡");
    list.add("大概就是");
    list.add("re公司");
    list.add("儿童哟普");
    list.add("第一桶金");
    list.add("第三方少打个");
    list.add("爽肤水");
    list.add("感慨太多");
    list.add("让他让");
    list.add("大概都符合");
    list.add("色粉管结合的");
    list.add("广告记录");

    for(int i=0;i<list.size();i++){
      String str =list.get(i);
      TextView tv =new TextView(getContext());
      tv.setSingleLine(true);
      tv.setGravity(Gravity.CENTER);
      tv.setTextColor(Color.BLACK);
      tv.setText(str);
      tv.setBackgroundDrawable(itemDrawable);
      MarginLayoutParams lps =new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      lps.setMargins(20,10,20,10);
      tv.setLayoutParams(lps);
      addView(tv);
    }
    viewGroups =new ArrayList<>();
  }
  private List<List<ViewControl>> viewGroups;
  private int width;
  public static final String TAG="TAG_ContentWrapView";
  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(viewGroups.size()!=0){
      viewGroups.clear();
    }
    int measureWidth =width=MeasureSpec.getSize(widthMeasureSpec);
    int curLineHeight =0;
    int curLineWidth =0;//当前行的宽度

    for(int i=0;i<getChildCount();i++){
      View child =getChildAt(i);
      MarginLayoutParams lps = (MarginLayoutParams) child.getLayoutParams();
      measureChild(child,widthMeasureSpec,heightMeasureSpec);

      int childWidth =child.getMeasuredWidth()+lps.leftMargin+lps.rightMargin;
      int childHeight =child.getMeasuredHeight();
      curLineWidth+=childWidth;

      if(curLineWidth>measureWidth){//如果当前值大于最大宽度，那么说明这个控件应该放在下一行去，那么下一行的第一个控件就是当前控件，同时，高度需要加上一个控件的高度
        curLineHeight+=childHeight+marTop;
        curLineWidth=childWidth;//
        /*List<ViewControl> list =viewGroups.get(viewGroups.size()-1);
        ViewControl viewCont =list.get(list.size()-1);//这儿是有风险的
        viewCont.rect.right+=marLeft;*/
        viewGroups.add(new ArrayList<>());//新添一行
      }
      if(viewGroups.size()==0){
        viewGroups.add(new ArrayList<>());
        curLineHeight+=childHeight+marTop;
      }
      ViewControl control =new ViewControl();
      control.child =child;
      control.rect =new Rect(curLineWidth-childWidth,curLineHeight-childHeight,curLineWidth,curLineHeight);

      viewGroups.get(viewGroups.size()-1).add(control);
    }
    setMeasuredDimension(width,curLineHeight);
  }
  @Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
    for(List<ViewControl> each:viewGroups){
      for(ViewControl child:each){
        String tag =((TextView)child.child).getText().toString();
        child.child.layout(child.rect.left,child.rect.top,child.rect.right,child.rect.bottom);
      }
    }
  }

  public void setList(List<String> list){
    this.list =list;
    requestLayout();
  }
  public static class ViewControl{
    public View child;
    public Rect rect;
  }
}
